package com.example.gaojt.androiddemo.video;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.MediaController;
import android.widget.VideoView;

import com.example.gaojt.androiddemo.R;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gaojt on 8/23/17.
 */

public class VideoViewDemo extends Activity {

    private static final String TAG = "VideoViewDemo";
    private VideoView mVideoView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_view_demo_ly);

        WebView webView = (WebView) findViewById(R.id.my_web_view);
        initWebSetting(webView);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onLoadResource(WebView view, String url) {
                super.onLoadResource(view, url);
                Log.d(TAG, "onLoadResource: url = " + url);
                if (url.contains("http://files.beedownloader.com/")) {
                    Log.d(TAG, "onLoadResource: url = " + url);
                    Uri uri = Uri.parse(url);
                    mVideoView.setVideoURI(uri);
                    mVideoView.start();
                }
            }
        });

        VideoView videoView = (VideoView) findViewById(R.id.my_video_view);
        mVideoView = videoView;
        MediaController mediaController = new MediaController(this);
//        Uri uri = Uri.parse("https://m.youtube.com/watch?v=KWw_5PYu58I");
//        Log.d(TAG, "onCreate: uri = " + uri);
//        videoView.setVideoURI(uri);
        videoView.setMediaController(mediaController);
        mediaController.setMediaPlayer(videoView);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Log.d(TAG, "onPrepared: ");
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "onCompletion: ");
            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                Log.d(TAG, "onError: what = " + what + ",extra = " + extra);
                return false;
            }
        });
        videoView.requestFocus();
//        videoView.start();

//        Map<String, String> extraHeaders = new HashMap<>();
//        extraHeaders.put("Referer", "http://m.xhubs.tv");
//        webView.loadUrl("http://m.xhubs.tv/detail/1642006", extraHeaders);
        webView.loadUrl("https://m.youtube.com/watch?v=KWw_5PYu58I&app=desktop");
    }

    public void initWebSetting(WebView webView) {
        webView.requestFocus();
        webView.setSelected(true);
        webView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setBlockNetworkImage(true);
        initCache(webView);
    }

    private void initCache(WebView webView) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setAppCacheEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
        webSettings.setAppCachePath(this.getDir("web_appcache", Context.MODE_PRIVATE).getPath());
    }
}
